<?php
/**
 * Date: 2017/11/10
 * Time: 10:08
 * Author: zeronlee
 */

namespace App\Http\Controllers\Api\V1\Credit;


use App\Http\Controllers\Api\V1\BaseController;
use App\Http\Controllers\Api\V1\TransformerController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;



class IndexController extends BaseController
{

    /**积分排行相关操作
     * @param Request $request
     * @return $this
     */
    public function index(Request $request)
    {


        $last=$this->yearCredit();

        exit();


    }

    /**
     * 个人中心积分（分值，时间，来源）
     */
    public function personalCenter()
    {
        $userid=6;  //当前用户的userid需要 替换
        $last = DB::table('reward_log')
            ->join('vuls', 'vuls.id', '=', 'reward_log.vuld_id')
            ->where([['reward_log.user_id', $userid], ['reward_log.type', 2]])
            ->orderBy('reward_log.id', 'desc')
            ->select('reward_log.*', 'vuls.title', 'vuls.vuls_number','vuls.description')
            ->paginate(10);
        return   $this->returnArray($last->toArray());

    }


    /**最近一次积分记录
     * @return mixed
     */
    public  function lastCredit(){

        $last = DB::table('credit_edition')
            ->orderBy('id', 'desc')
         ->first();
       return $last;
    }

    /**
     * 前三名排序
     */
    public  function topThree()
    {
        $last = DB::table('credit_edition')
            ->join('users', 'users.id', '=', 'credit_edition.user_id')
            ->leftJoin('user_ext', 'credit_edition.user_id', '=', 'user_ext.user_id')
            ->orderBy('credit_edition.year', 'desc')
            ->orderBy('credit_edition.month', 'desc')
            ->orderBy('credit_edition.month_credit','desc')
            ->orderBy('credit_edition.year_credit','desc')
            ->orderBy('credit_edition.credit','desc')
            ->orderBy('credit_edition.id','desc')
            ->limit(3)
            ->select("users.name as username","user_ext.avatar")
            ->get();
            return   $this->returnArray($last->toArray());

    }

    /**月度积分排行榜
     * @return array
     */
    public  function monthCredit(Request $request){
        $year=$request->year?$request->year:date("Y");//某年的月度排行榜
        $month=$request->month?$request->month:date("m");//某月的排行榜
        $where=array();
        if($request->category){
            $where["category_id"]= $request->category;
        }
        $last = DB::table('credit_edition')
            ->where($where)
            ->leftJoin('user_ext', 'credit_edition.user_id', '=', 'user_ext.user_id')
            ->leftJoin('users', 'users.id', '=', 'user_ext.user_id')
            ->where([['credit_edition.month', $month], ['credit_edition.year', $year]])
            ->orderBy('credit_edition.month_credit', 'desc')
            ->orderBy('credit_edition.id','desc')
            ->select("credit_edition.month_credit","credit_edition.category_id","credit_edition.year","credit_edition.month","credit_edition.id","credit_edition.user_id","user_ext.team","users.name as username")
            ->paginate(10);

        $order=array();
        $new=array();
        foreach ($last as $value)
        {
            $key=null;
            $current=$value->month_credit;
            if(!in_array($current, $order)){
                $order[]= $current;
                $key=array_search($current, $order);
                $value->order=$key+1;
            }else{
                $key=array_search($current, $order);
                $value->order=$key+1;
            }
            $new[]=$value;
        }
        $newdata['data']=$new;
        $return=array_merge($last->toArray(),$newdata);
        return   $this->returnArray($return);
    }

    /**年度积分排行榜
     * @return array
     */
    public  function yearCredit(Request $request){
        $year=$request->year?$request->year:date("Y");//某年的月度排行榜
        $where=array();
        if($request->category){
            $where["category_id"]= $request->category;
        }
        $last = DB::table('credit_edition')
            ->where($where)
            ->leftJoin('user_ext', 'credit_edition.user_id', '=', 'user_ext.user_id')
            ->leftJoin('users', 'users.id', '=', 'user_ext.user_id')
            ->where('credit_edition.year', $year)
            ->orderBy('credit_edition.year_credit', 'desc')
            ->orderBy('credit_edition.id','desc')
            ->select("credit_edition.year_credit","credit_edition.category_id","credit_edition.year","credit_edition.id","credit_edition.user_id","user_ext.team","users.name as username")
            ->paginate(10);


        $order=array();
        $new=array();
        $user=array();
        foreach ($last as $value)
        {
            $key=null;
            $current=$value->year_credit;
            $current_user=$value->user_id;
            if(!in_array($current_user, $user)){
                if(!in_array($current, $order)){
                    $order[]= $current;
                    $key=array_search($current, $order);
                    $value->order=$key+1;
                }else{
                    $key=array_search($current, $order);
                    $value->order=$key+1;
                }
                $user[]=$current_user;
                $new[]=$value;
            }
        }
        $newdata['data']=$new;
        $return=array_merge($last->toArray(),$newdata);
            return  $this->returnArray($return);

    }


    /**总排行榜
     * @return array
     */
    public  function credit(Request $request){
        $where=array();
        if($request->category){
            $where["category_id"]= $request->category;
        }
        $last = DB::table('credit_edition')
            ->where($where)
            ->leftJoin('user_ext', 'credit_edition.user_id', '=', 'user_ext.user_id')
            ->leftJoin('users', 'users.id', '=', 'user_ext.user_id')
            ->orderBy('credit_edition.credit', 'desc')
            ->orderBy('credit_edition.id','desc')
            ->select("credit_edition.credit","credit_edition.category_id","credit_edition.id","credit_edition.user_id","user_ext.team","users.name as username")
            ->paginate(10);
        $order=array();
        $new=array();
        $user=array();
        foreach ($last as $value)
        {
            $key=null;
            $current=$value->credit;
            $current_user=$value->user_id;
            if(!in_array($current_user, $user)){
                if(!in_array($current, $order)){
                    $order[]= $current;
                    $key=array_search($current, $order);
                    $value->order=$key+1;
                }else{
                    $key=array_search($current, $order);
                    $value->order=$key+1;
                }
                $user[]=$current_user;
                $new[]=$value;
            }
        }
        $newdata['data']=$new;
        $return=array_merge($last->toArray(),$newdata);
        return  $this->returnArray($return);
    }


    /**漏洞类型
     * @return array
     */
    public  function category(){

        $last = DB::table('category')
            ->whereNull('deleted_at')
            ->select("id","name")
            ->get();
        return  $this->returnArray($last->toArray());

    }
}